//
//    Tokaplot
//    https://code.google.com/p/tokaplot/
//
//    License: GPL v3
//    https://code.google.com/p/tokaplot/wiki/License
//
//    This file is part of Tokaplot.
//
//    Tokaplot is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Tokaplot is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Tokaplot.  If not, see <http://www.gnu.org/licenses/>
//

package com.tokaplot;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.regex.Pattern;

//import android.util.Log;

// Class for reading network information
public class ReadNetwork {
	
	//private static final String TAG = "Tokaplot";
	
	boolean fileExists = false;
	
	// Last values
	private boolean nt_Init = true;
	private long nt_last_Rec = 0;
	private long nt_last_Trans = 0;	
	
	// Pattern matchers
	private static Pattern pNet = Pattern.compile("lo.*|usb.*|dummy.*");
	private static Pattern pSplit = Pattern.compile("\\s+|:\\s*");
	
	public ReadNetwork() {
		
		// Try and open file
		File fNet = new File("//proc//net//dev");
    	if (fNet.exists() == false) {
    		fNet = null;
    		fileExists = false;
    	} else {
    		fileExists = true;
    	}
	}
	
	public boolean canRead() {
    	return fileExists;
	}
	
	public boolean read(float [] net_val) {
		
    	// Network data
		File fNet = new File("//proc//net//dev");
    	if (fNet.exists() == true) {
			try {

			    int nLine = 0;
			    long nrRecieve = 0;
			    long nrTransmit = 0;
			    String line;
			    String [] lineElements;
			    
				//Read file
			    BufferedReader brNet = new BufferedReader(new FileReader(fNet), 1000);				
			    while((line = brNet.readLine()) != null) {
			    	if (nLine > 1) {
			    		line=line.trim();
					    
					    // Check line head
					    if (pNet.matcher(line).matches() == false) {
					    	lineElements = pSplit.split(line, 11);
					    	nrRecieve += Long.parseLong(lineElements[1]);
					    	nrTransmit += Long.parseLong(lineElements[9]);
					    }
			    	}
			    	nLine++;
			    }
			    brNet.close();
			    
			    // Calculate differences
			    if (nt_Init == false) {
				    
			    	net_val[0]=(float)(nrRecieve - nt_last_Rec);
			    	net_val[1]=(float)(nrTransmit - nt_last_Trans);
			    	
			    	// Check for negative difference if an adapter has been switched off
			    	if (net_val[0] < 0.0f || net_val[1] < 0.0f) {
			    		
			    		net_val[0] = 0.0f;
			    		net_val[1] = 0.0f;
			    	
			    	}
			    }
			    
			    // Save last values
			    nt_last_Rec = nrRecieve;
			    nt_last_Trans = nrTransmit;			    
			    nt_Init = false;
			    
			    return true;
			}
			catch (Exception e) {
				
				// Can't read file
				//Log.e(TAG,"ReadNetwork: Exception reading data file: " + e.getMessage());
			}
    	}
    	
    	// Can't find or open file
    	nt_Init = true;
    	return false;
	}
	
}
